<?php

/**
 * Class SiteController
 *
 * @author Hua Yang <htmlcook@gmail>
 * @since 1.0
 */
class SiteController extends BController
{
	public $layout = 'site';

	public function actions()
	{
		return array(
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
		);
	}

	/**
	 * 首页
	 */
	public function actionIndex()
	{
		$this->layout = 'main';
		$this->render('index');
	}

	public function actionLogin()
	{
		if (!Yii::app()->getUser()->getIsGuest()) {
			$this->redirect(Yii::app()->getUser()->getReturnUrl());
		}

		$model = new LoginForm;

		if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();

		}

		if (isset($_POST['LoginForm'])) {
			$model->attributes = $_POST['LoginForm'];
			if ($model->validate() && $model->login()) {
				$this->redirect(Yii::app()->getUser()->returnUrl);
			}
		}

		$this->render('login', array('model' => $model));
	}

	/**
	 * 退出
	 */
	public function actionLogout()
	{
		Yii::app()->getUser()->logout();
		$this->redirect(array('login'));
	}

	/**
	 * 找回密码
	 *
	 * @throws CException
	 */
	public function actionLostPassword()
	{
		if (!Yii::app()->getUser()->getIsGuest()) {
			$this->redirect(Yii::app()->getUser()->getReturnUrl());
		}

		if (!Yii::app()->hasComponent('mail')) {
			throw new CException('应用程序没有配置邮件服务，不能找回密码。');
		}

		if (!Yii::app()->option->get('enableBackendForgetPassword')) {
			throw new CException('应用程序没有开启找回密码。');
		}

		$model = new LostPasswordForm;

		if (isset($_POST['LostPasswordForm'])) {
			$model->attributes = $_POST['LostPasswordForm'];
			if ($model->validate() && $model->sendMail()) {
				Yii::app()->getUser()->setFlash('success', '重置密码邮件已发送至您的电子邮箱。');
				$this->redirect(array('login'));
			}
		}

		$this->render('lostPassword', array('model' => $model));
	}

	/**
	 * 重置密码
	 *
	 * @param $token
	 */
	public function actionResetPassword($token)
	{
		if (!Yii::app()->getUser()->getIsGuest()) {
			$this->redirect(Yii::app()->getUser()->getReturnUrl());
		}

		$model = new ResetPasswordForm($token);

		if (!$model->getUser()) {
			Yii::app()->getUser()->setFlash('error', '无效的重置令牌，请重新获取重置密码邮件');
			$this->redirect(array('lostPassword'));
		}

		if (isset($_POST['ResetPasswordForm'])) {
			$model->attributes = $_POST['ResetPasswordForm'];
			if ($model->validate()) {
				if ($model->resetPassword()) {
					Yii::app()->getUser()->setFlash('success', '重置密码成功');
					$this->redirect(array('login'));
				} else {
					Yii::app()->getUser()->setFlash('error', '重置密码失败');
				}
			}
		}

		$this->render('resetPassword', array('model' => $model));
	}

	/**
	 * 设置
	 *
	 * @param string $category
	 * @throws CHttpException
	 */
	public function actionSetting($category = 'general')
	{
		$this->layout = 'main';
		try {
			$model = new Setting($category);
		} catch (CException $e) {
			throw new CHttpException(404, $e->getMessage());
		}

		$categories = array();

		if (isset($_POST['Setting'])) {
			$model->attributes = $_POST['Setting'];
			if ($model->save()) {
				Yii::app()->getUser()->setFlash('success', $model->getTitle() . '已保存');
				$this->redirect(array('setting', 'category' => $category));
			}
		}

		$this->render('setting', array('model' => $model, 'category' => $category, 'categories' => $categories));
	}

	/**
	 * 清空缓存
	 */
	public function actionFlushCache()
	{
		ignore_user_abort(true);
		@set_time_limit(0);
		Yii::app()->getCache()->flush();
		Yii::app()->getUser()->setFlash('success', '站点缓存已清空');
		$this->redirect(array('setting', 'category' => 'general'));
	}

	public function actionShowLog($file='application')
	{
		$path = Yii::app()->getRuntimePath();
		echo '<meta charset="' . Yii::app()->charset . '">';
		echo '<pre>';
		echo file_get_contents($path . '/' . $file . '.log');
		echo '</pre>';
	}
}
